﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Resources;
using Cesave.Domain.Core;
using System.Data;

namespace Cesave.Domain.PermisosModule.Contracts.AreaSecciones
{
	/// <summary>
	/// 
	/// </summary>
	public class AreaSeccionService
		: IAreaSeccionService
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private IAreaSeccionRepository _areaSeccionRepository;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="areaSeccionRepository"></param>
		public AreaSeccionService(IAreaSeccionRepository areaSeccionRepository)
		{
			if (areaSeccionRepository == null)
				throw new ArgumentNullException(Messages.exception_DependenciesAreNotInitialized, "areaSeccionRepository");
			_areaSeccionRepository = areaSeccionRepository;
		}

		#endregion

		#region IAreaSeccionService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="areaSeccion"></param>
		public void AddAreaSeccion(Entities.AreaSeccion areaSeccion)
		{
			if (areaSeccion == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "areaSeccion");
			IUnitOfWork unitOfWork = _areaSeccionRepository.StoreContext;
			_areaSeccionRepository.Add(areaSeccion);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="areaSeccion"></param>
		public void ChangeAreaSeccion(Entities.AreaSeccion areaSeccion)
		{
			if (areaSeccion == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "areaSeccion");
			IUnitOfWork unitOfWork = _areaSeccionRepository.StoreContext;
			_areaSeccionRepository.Modify(areaSeccion);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="areaGuid"></param>
		/// <param name="juntaGuid"></param>
		/// <returns></returns>
		public List<Entities.AreaSeccion> FindAreaSeccionesByArea(Guid juntaGuid, Guid areaGuid)
		{
			if (juntaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "juntaGuid");
			if (areaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "areaGuid");
			AreaSeccionesByAreaSpecification areaSpecification = new AreaSeccionesByAreaSpecification(areaGuid);
			AreaSeccionesByJuntaSpecification juntaSpecification = new AreaSeccionesByJuntaSpecification(juntaGuid);
			return _areaSeccionRepository.FindAreaSeccionesByJuntaAndArea(juntaSpecification, areaSpecification).ToList();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaGuid"></param>
		/// <param name="areaGuid"></param>
		/// <returns></returns>
		public List<Entities.AreaSeccion> FindAreaSeccionesByJuntaAndArea(Guid juntaGuid, Guid areaGuid)
		{
			if (juntaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "juntaGuid");
			if (areaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "areaGuid");
			AreaSeccionesByJuntaSpecification juntaSpecification = new AreaSeccionesByJuntaSpecification(juntaGuid);
			AreaSeccionesByAreaSpecification areaSpecification = new AreaSeccionesByAreaSpecification(areaGuid);
			return _areaSeccionRepository.FindAreaSeccionesByJuntaAndArea(juntaSpecification, areaSpecification).ToList();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaGuid"></param>
		/// <param name="areaGuid"></param>
		/// <returns></returns>
		public List<Entities.AreaSeccion> FindAreaSeccionesForExpedicion(Guid juntaGuid, Guid areaGuid)
		{
			if (juntaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "juntaGuid");
			if (areaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "areaGuid");
			AreaSeccionesForExpedicionSpecification specification = new AreaSeccionesForExpedicionSpecification(juntaGuid, areaGuid);
			return _areaSeccionRepository.FindAreaSeccionesForExpedicion(specification).ToList();
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			if (_areaSeccionRepository != null && _areaSeccionRepository.StoreContext != null)
				_areaSeccionRepository.StoreContext.Dispose();
		}

		#endregion
	}
}
